import com.sun.xml.internal.bind.v2.TODO;

public class Code2infrared {
    /*
     * 16进制的值转为二进制的红外码值:8位
     * */
    private static  String hex2binary(String hexStr){

        String a = Integer.toBinaryString(Integer.parseInt(hexStr,16));//字符16进制整数转为二进制字符串
        System.out.println(a);
        a= new StringBuilder(a).reverse().toString();//逆转码
        //补助8位
        switch (a.length()){
            case 1:
                a = a+"0000000";
                System.out.println(a);
                break;
            case 2:
                a=a+"000000";
                System.out.println(a);
                break;
            case 3:
                a=a+"00000";
                System.out.println(a);
                break;
            case 4:
                a=a+"0000";
                System.out.println(a);
                break;
            case 5:
                a=a+"000";
                System.out.println(a);
                break;
            case 6:
                a=a+"00";
                System.out.println(a);
                break;
            case 7:
                a=a+"0";
                System.out.println(a);
                break;
            default:
                System.out.println(a);
        }
        return a;
    }
    /*
     * 二进制转为16位红外发送值
     * */
    private static int[] binary2byte(String strBit,int status){
        //1
        int oneUp = 560;
        int oneDown = 1680;

        //0
        int zeroUp = 560;
        int zeroDown = 560;

        //状态
        int zheng = 1;
        int fan = -1;
        //
        int[] base = new int[16];
        int baseCount = 0;//base数组计算
        //二进制转为数组
        String[] str = strBit.split("");
        for(int count=0;count<=str.length-1;count++){

            if(str[count].equals("1")){//二进制1
                if(status==zheng){//正码
                    base[baseCount]=oneUp;
                    baseCount += 1;
                    base[baseCount]= oneDown;
                    baseCount += 1 ;
                }else if(status==fan){//反码
                    base[baseCount]=zeroDown;
                    baseCount += 1;
                    base[baseCount]= zeroDown;
                    baseCount += 1 ;
                }

            }else if(str[count].equals("0")){//二进制 0

                if(status==fan){//反码
                    base[baseCount]=oneUp;
                    baseCount += 1;
                    base[baseCount]= oneDown;
                    baseCount += 1 ;
                }else if(status==zheng){//正码
                    base[baseCount]=zeroDown;
                    baseCount += 1;
                    base[baseCount]= zeroDown;
                    baseCount += 1 ;
                }
            }
        }
        return base;
    }

    /*
     * 客户码转为红外发送码值
     * */
    private static int[] clientCode(String code1, String code2){
        int[] baseClientCode = new int[32];
        // TODO: 2018/7/7 客户码转码位置
        int[] clientCode1 = binary2byte(hex2binary(code1),1);
        int[] clientCode2 = binary2byte(hex2binary(code2),1);

        for(int i = 0;i<=clientCode1.length-1;i++){
            baseClientCode[i] = clientCode1[i];
            baseClientCode[16+i] = clientCode2[i];
        }
        return baseClientCode;
    }

    /*
     * 数据码转为红外发送码值
     * */
    private static int[] dataCode(String code){
        // TODO: 2018/7/7 数据码转码位置
        int[] zhengdata = binary2byte(hex2binary(code),1);
        int[] fandata = binary2byte(hex2binary(code),-1);
        /*int[][] basedataCode = new int[2][8];
        for(int i=0;i<=zhengdata.length-1;i++){
            basedataCode[0][i]=zhengdata[i];
            basedataCode[1][i]=fandata[i];
        }*/

        int[] basedataCode = new int[32];
        for(int i = 0;i<=zhengdata.length-1;i++){
            basedataCode[i] = zhengdata[i];
            basedataCode[16+i] = fandata[i];
        }
        return basedataCode;
    }

    /*
     * 整合全部码值到一个数组,可以直接发送使用
     * */
    public static int[] allCode(String clientCode1, String clientCode2, String dataCode){
        int[] baseAll = new int[68];
        // TODO : 整合数据
        //Start code
        int start1 = 9000;
        int start2 = 4500;

        //End code
        int end1 = 560;
        int end2 = 20000;

        //get Client code
        int[] client = clientCode(clientCode1,clientCode2);
        int[] data = dataCode(dataCode);//融合了正反
        //整合数据
        baseAll[0] = start1;
        baseAll[1] = start2;
        baseAll[66] = end1;
        baseAll[67] = end2;

        for(int i = 0;i<= client.length-1;i++){
            baseAll[i+2]= client[i];
            baseAll[i+34] = data[i];
        }
        return baseAll;
    }

}
